package bog

class Thread {
    /** ***********************************/
    /** relationships                   **/
    /** ***********************************/
    static hasOne = [
            question: Question,
            course: Course,
            status: ThreadStatus
    ]

    static hasMany = [
            answers: Answer,
            tags: Tag
    ]
    /** ***********************************/

    static mapping = {answers sort: 'id', order: 'asc'}

    static constraints = {
        question(nullable: false)
    }

/*
    static mapping = {
        status defaultvalue: ThreadStatus.OPEN
    }
*/



    Thread(question, tags, course) {
        def tempTags = []

        tags?.each {tempTags.add(Tag.get(it))}

        this.tags = tempTags
        this.course = Course.get(course)
        this.question = question
        this.status = ThreadStatus.OPEN
    }

//    List<Answer> getAnswers() {
//        println "yo"
//        return answers[1..-1]
//        // return answers.asList().subList(1, answers.size())
//    }

    @Override
    public java.lang.String toString() {
        return "Thread{" +
                "answers=" + answers +
                ", course=" + course +
                ", question=" + question +
                ", tags=" + tags +
                ", status=" + status +
                '}';
    }

    public void solved() {
        this.status = ThreadStatus.SOLVED
    }

    public void close() {
        this.status = ThreadStatus.CLOSED
    }

//    Answer getBestAnswer() {
//        answers.asList().max()
//
//
////        return
//    }

}
